﻿Imports System
Imports System.Net
Imports System.IO
Imports System.Threading
Imports System.Text

Public Class FtpMod
    Public Shared Function Upload(username As String, password As String, uri As String, filename As String) As Boolean
        Dim fullpath As String
        Dim fileInf As FileInfo = New FileInfo(filename)
        fullpath = uri + fileInf.Name
        Dim reqFtp As FtpWebRequest
        Try
            reqFtp = FtpWebRequest.Create(New Uri(fullpath))
            reqFtp.Credentials = New NetworkCredential(username, password)
            reqFtp.KeepAlive = False
            reqFtp.UsePassive = False
            reqFtp.Method = WebRequestMethods.Ftp.UploadFile
            reqFtp.UseBinary = True
            reqFtp.ContentLength = fileInf.Length
            Dim b(1024) As Byte
            Dim contenlen As Integer
            Dim fs As FileStream = File.OpenRead(filename)
            Dim strm As Stream = reqFtp.GetRequestStream()
            contenlen = fs.Read(b, 0, 1024)
            While contenlen <> 0
                strm.Write(b, 0, contenlen)
                contenlen = fs.Read(b, 0, 1024)
            End While
            strm.Close()
            fs.Close()
            Return True
        Catch ex As Exception
            Return False
        End Try

    End Function

    Public Shared Function GetFileList(username As String, password As String, uri As String, mask As String) As String()
        ' Dim downloadFiles As String()
        Dim result As StringBuilder = New StringBuilder()
        Dim reqFTP As FtpWebRequest
        Try
            reqFTP = CType(FtpWebRequest.Create(New Uri(uri)), FtpWebRequest)
            reqFTP.UseBinary = True
            reqFTP.Credentials = New NetworkCredential(username, password)
            reqFTP.KeepAlive = False
            reqFTP.UsePassive = False
            reqFTP.Method = WebRequestMethods.Ftp.ListDirectory
            Dim response As WebResponse = reqFTP.GetResponse()
            Dim reader As StreamReader = New StreamReader(response.GetResponseStream())
            Dim line As String = reader.ReadLine()
            While line <> Nothing
                If mask.Trim() <> String.Empty And mask.Trim() <> "*.*" Then
                    Dim mask_ As String
                    mask_ = mask.Substring(0, mask.IndexOf("*"))
                    If line.Substring(0, mask_.Length) = mask_ Then
                        result.Append(line)
                        result.Append(vbLf)
                    End If
                Else
                    result.Append(line)
                    result.Append(vbLf)
                End If
                line = reader.ReadLine()

            End While
            result.Remove(result.ToString().LastIndexOf(vbLf), 1)
            reader.Close()
            response.Close()
            Return result.ToString().Split(vbLf)

        Catch ex As Exception
            MessageBox.Show(ex.Message)
            'Return downloadFiles
        End Try
    End Function

    Public Shared Function Download(ByVal username As String, ByVal password As String, ByVal uri As String, ByVal filename As String) As Boolean
        Dim fullpath As String
        Dim fileInf As FileInfo = New FileInfo(filename)
        fullpath = uri + fileInf.Name
        Dim reqFtp As FtpWebRequest
        Dim outputStream As FileStream
        Try
            reqFtp = FtpWebRequest.Create(New Uri(fullpath))
            reqFtp.Credentials = New NetworkCredential(username, password)
            reqFtp.KeepAlive = False
            reqFtp.UsePassive = False
            reqFtp.Method = WebRequestMethods.Ftp.DownloadFile
            reqFtp.UseBinary = True
            'Dim cl As Long
            'cl = reqFtp.GetResponse().ContentLength
            Dim response As FtpWebResponse
            response = CType(reqFtp.GetResponse(), FtpWebResponse)
            Dim strm As Stream = response.GetResponseStream()
            'Dim strm As Stream = reqFtp.GetRequestStream()

            Dim b(2048) As Byte
            Dim readCount As Integer
            outputStream = New FileStream(filename, FileMode.Create)
            readCount = strm.Read(b, 0, 2048)
            While readCount > 0
                outputStream.Write(b, 0, readCount)
                readCount = strm.Read(b, 0, 1024)
            End While
            strm.Close()
            outputStream.Close()
            Return True
        Catch ex As Exception
            'MessageBox.Show(ex.Message)
            Return False
        End Try
    End Function
End Class
